package com.wq.wrongquestion.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wq.wrongquestion.entity.pol.PoliticsEntity;
import com.wq.wrongquestion.service.pol.PoliticsService;
import com.wq.wrongquestion.utils.FileUploadUtils;

@Controller
@RequestMapping("/pol")
public class PoliticsController {
    /************************************************Politics Start************************************************/

    @Autowired
    public PoliticsService politicsService;

    @Autowired
    public FileUploadUtils fileUploadUtils;

    final String IMG_PAHT_YT_POL = "images/pol/yt/";
    final String IMG_PAHT_DA_POL = "images/pol/da/";

    @RequestMapping("/politicsInit") 
    public String politicsInit(Model model){
        //章节
        List<String> allZj=politicsService.getPoliticsZj();
        
        model.addAttribute("allZj", allZj);
        model.addAttribute("condEntity", new PoliticsEntity());
        
        return "pol/politicsSearch";
    }


    @PostMapping("/politicsIns")
    public String insPolitics(PoliticsEntity politicsEntity, @RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile, Model model) {
        // 获取文件名
        String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
        String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();

        // 获取文件路径
        String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_POL);
        String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_POL);

        // 保存文件
        try {
            if (ytFile.getSize() > 0) {
                fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                politicsEntity.setImgytnm(fileName);
                politicsEntity.setImgytpath(filePath);
            }
            if (daFile.getSize() > 0) {
                fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                politicsEntity.setImgdanm(fileName2);
                politicsEntity.setImgdapath(filePath2);
            }
            // 插入数据
            politicsEntity.setFxcs(1);
            politicsEntity.setXueke(politicsEntity.getHidXueke());

            politicsService.insPolitics(politicsEntity);

            // return "redirect:/pol/politicsInit";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "发生错误,请重试!");
            return "redirect:/pol/addPolitics";
        }
        return "redirect:/pol/politicsInit";
    }
    @RequestMapping("/addPolitics")
    public String addPolitics(Model model){
        List<String> allZj=politicsService.getPoliticsZj();
        
        model.addAttribute("allZj", allZj);
        return "pol/addPolitics";
    }

    @RequestMapping("/searchPolitics")
    public String searchPolitics(Model model, @RequestParam(value = "pageNm",defaultValue= "1") Integer pageNum,
            @RequestParam(value="pageSize" ,defaultValue= "10") Integer pageSize, PoliticsEntity politicsEntity) {

        PageInfo<PoliticsEntity> politics = politicsService.getPolitics(pageNum, pageSize, politicsEntity);
        List<String> allZj=politicsService.getPoliticsZj();

        model.addAttribute("allZj", allZj);
        model.addAttribute("politics", politics);
        model.addAttribute("condEntity", politicsEntity);

        return "pol/politicsSearch";
    }

    @RequestMapping("/searchPoliticsByID")
    public String searchPoliticsByID(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {

        List<PoliticsEntity> politicsByID = politicsService.getPoliticsByID(id);
        List<String> allZj=politicsService.getPoliticsZj();
        if(!politicsByID.isEmpty()){
            model.addAttribute("politics", politicsByID);
        }else{
            model.addAttribute("msg", "这条记录在数据库中不存在！");
            return "pol/politicsSearch";
        }
        
        model.addAttribute("allZj", allZj);

        return "pol/modifyPolitics";
    }

    @PostMapping("/politicsUpd")
    public String politicsUpd(Model model, PoliticsEntity politicsEntity,@RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile) {
        try {
            // 图片比较
            if(ytFile != null && ytFile.getOriginalFilename() !=null){
                if(!ytFile.getOriginalFilename().startsWith("yt", 0)){
                    // 获取文件名
                    String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_POL);
                    if (ytFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                        politicsEntity.setImgytnm(fileName);
                        politicsEntity.setImgytpath(filePath);
                    }
                }
            }
            // 图片比较
            if(daFile != null && daFile.getOriginalFilename() !=null){
                if(!daFile.getOriginalFilename().startsWith("da", 0)){
                    // 获取文件名
                    String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_POL);
                    if (daFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                        politicsEntity.setImgdanm(fileName2);
                        politicsEntity.setImgdapath(filePath2);
                    }                
                }
            }
            // 插入数据
            politicsEntity.setFxcs(1);
            politicsEntity.setXueke(politicsEntity.getHidXueke());

            Integer updRes=politicsService.updPolitics(politicsEntity);

            if(updRes==1){
                model.addAttribute("msg", "修改成功！");
            }else{
                model.addAttribute("msg", "修改失败！");
            } 
            List<PoliticsEntity> politicsByID = politicsService.getPoliticsByID(politicsEntity.getId());           
            if(politicsByID.isEmpty()){
               politicsByID = new ArrayList<PoliticsEntity>();
            }
            model.addAttribute("politics", politicsByID);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "文件上传发生错误,请重试!");
            
        }


        return "pol/modifyPolitics";   
    }

    @PostMapping("/politicsDel")
    public String politicsDel(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {
        politicsService.delPolitics(id);
        
        return "redirect:/pol/politicsInit";
    }    
    /************************************************Politics End************************************************/
}
